package com.cxrj.zhsq.redCommunity.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.redCommunity.dao.VolunteerEnrollMapper;
import com.cxrj.zhsq.redCommunity.dao.VolunteerRecruitMapper;
import com.cxrj.zhsq.redCommunity.entity.VolunteerEnroll;
import com.cxrj.zhsq.redCommunity.entity.VolunteerRecruit;
import com.cxrj.zhsq.redCommunity.service.VolunteerRecruitService;
import com.cxrj.zhsq.userManage.dao.AppUserMapper;
import com.cxrj.zhsq.userManage.entity.AppUser;
import com.cxrj.zhsq.utils.HtmlUtil;
import com.cxrj.zhsq.utils.IpUtil;
import com.cxrj.zhsq.utils.SqlCondition;
/**
 * 志愿者招募业务处理层
 * @author wangm
 * @created @2018年5月17日-上午9:26:51
 *
 */
@Service
public class VolunteerRecruitServiceImpl implements VolunteerRecruitService {
	
	@Resource
	private VolunteerRecruitMapper volunteerRecruitMapper;
	
	@Resource
	private VolunteerEnrollMapper volunteerEnrollMapper;
	
	@Resource
	private AppUserMapper appUserMapper;
	
	@Override
	public Map<String, Object> selectVolunteerRecruitList(HttpServletRequest request, HttpServletResponse response) {
		volunteerRecruitMapper.updateRecruiByTime();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = SqlCondition.getBaseCondition(request, response);
		String state=request.getParameter("state");
		//查询条件
		String kod=request.getParameter("kod");
		String kodType=request.getParameter("kodType");
		String sqlStr = "";
		
		if(kodType != null && !kodType.equals("")){
			sqlStr += "and recruit_state = " + kodType;
		}
		
		if(kod != null && !kod.equals("")){
			sqlStr += " and ( recruit_info_theme like " + '"' + '%' + kod + '%' + '"'+')';
		}
		sqlMap.put("recruitState", state);
		sqlMap.put("condition",sqlStr);
		int count = volunteerRecruitMapper.selectCount(sqlMap);
		List<VolunteerRecruit> list = volunteerRecruitMapper.selectVolunteerRecruitList(sqlMap);
		resultMap.put("total", count);
		resultMap.put("recordsFiltered", count);
		resultMap.put("rows", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectVolunteerRecruitById(String vrId, String userId, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		VolunteerRecruit volunteerRecruit = volunteerRecruitMapper.selectByPrimaryKey(vrId);
		if (volunteerRecruit.getS1() != null && !volunteerRecruit.getS1().equals("")) {
			volunteerRecruit.setS1(HtmlUtil.getAbsSource(volunteerRecruit.getS1(), "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/"));
		}
		resultMap.put("result", volunteerRecruit);
		if(userId!=null) {
			sqlMap.put("vrId", vrId);
			sqlMap.put("userId", userId);
			sqlMap.put("veVrid", vrId);
			Integer isSign = volunteerEnrollMapper.selectEnrollByIsSign(sqlMap);
			resultMap.put("isSign", isSign);
			resultMap.put("list", volunteerEnrollMapper.selectList(sqlMap));
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> insertVolunteerRecruit(VolunteerRecruit modle, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		modle.setVrId(UUID.randomUUID().toString());
		modle.setRecruitStartTime(new Date());
		int n = volunteerRecruitMapper.insertSelective(modle);
		resultMap.put("result", n);
		return resultMap;
	}

	@Override
	public Map<String, Object> updateVolunteerRecruit(VolunteerRecruit modle, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int n = volunteerRecruitMapper.updateByPrimaryKeySelective(modle);
		resultMap.put("result", n);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectVolunteerEnroll(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("veVrid", request.getParameter("vrId"));
		List<VolunteerEnroll> list = volunteerEnrollMapper.selectList(map);
		resultMap.put("result", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectVolunteerRecrui(String dataIndex, String pageCount) {
		volunteerRecruitMapper.updateRecruiByTime();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("start", Integer.parseInt(dataIndex));
		sqlMap.put("length", Integer.parseInt(pageCount));
		resultMap.put("list", volunteerRecruitMapper.selectVolunteerRecrui(sqlMap));
		return resultMap;
	}

	@Override
	public Map<String, Object> insertVolunteerEnroll(String id, String userId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AppUser appUser = appUserMapper.selectByPrimaryKey(userId);
		VolunteerEnroll volunteerEnroll = new VolunteerEnroll();
		volunteerEnroll.setVeId(UUID.randomUUID().toString());
		volunteerEnroll.setCreateTime(new Date());
		volunteerEnroll.setEnrollAddress(appUser.getUserAddress());
		volunteerEnroll.setEnrollIdcard(appUser.getUserIdCard());
		volunteerEnroll.setEnrollName(appUser.getUserName());
		volunteerEnroll.setEnrollTel(appUser.getUserMobile());
		volunteerEnroll.setEnrollTime(new Date());
		volunteerEnroll.setEnrollUserId(userId);
		volunteerEnroll.setVeVrid(id);
		Integer c = volunteerEnrollMapper.insertSelective(volunteerEnroll);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> updateVolunteerEnroll(String id, String userId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("userId", userId);
		sqlMap.put("id", id);
		Integer c = volunteerEnrollMapper.deleteBySign(sqlMap);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectVolunteerRecruiByMy(String userId, String status, String dataIndex, String pageCount) {
		volunteerRecruitMapper.updateRecruiByTime();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("start", Integer.parseInt(dataIndex));
		sqlMap.put("length", Integer.parseInt(pageCount));
		sqlMap.put("userId", userId);
		sqlMap.put("status", status);
		resultMap.put("list", volunteerRecruitMapper.selectVolunteerRecruiByMy(sqlMap));
		return resultMap;
	}

	@Override
	public Map<String, Object> selectVolunteerMien(String name, String dataIndex, String pageCount) {
		volunteerRecruitMapper.updateRecruiByTime();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("start", Integer.parseInt(dataIndex));
		sqlMap.put("length", Integer.parseInt(pageCount));
		sqlMap.put("name", name);
		resultMap.put("list", volunteerRecruitMapper.selectVolunteerRecruiMien(sqlMap));
		return resultMap;
	}
}
